<template>
    <el-container class="user-management">
        <el-header class="user-management__header">
            <div class="user-management__header-main">
                <el-input v-model="searchKey" placeholder="请输入账号名称" class="management__headerInput" :clearable="true"
                    style="width: 300px"></el-input>
                <el-button type="primary" size="small" style="margin-right: 88px" @click="search">搜索</el-button>
                <el-button type="success" size="small" @click="opeUserModal('add')">新增用户</el-button>
            </div>
        </el-header>
        <el-main class="user-management__content">
            <div ref="container" class="table-container">
                <el-table ref="table" :data="users" style="width: 100%" align="center" header-align="center">
                    <el-table-column label="账号" prop="name" />
                    <el-table-column label="密码" prop="password" />

                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button v-if="userInfo.id !== scope.row.id" type="danger" size="small"
                                @click="deleteUser(scope.row)">删除</el-button>
                            <el-button type="primary" size="small" @click="opeUserModal('edit', scope.row)">修改密码</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </el-main>
        <div class="pagination-container">
            <el-pagination :current-page="pageNum" :page-size="pageSize" layout="prev, pager, next" :total="total"
                background @current-change="handlePageChange" />
        </div>
        <dialog-edit :visible.sync="visible" v-if="visible" @refresh="refresh" ref="edit"></dialog-edit>
    </el-container>
</template>

<script>
import dialogEdit from "./components/account-edit.vue";
import { DEFAULTAVATAR } from "@/utils/const.js";
import { getUser } from "@/utils/auth";

export default {
    name: "userList",
    components: {
        dialogEdit
    },
    data() {

        return {
            userInfo: getUser(),
            visible: false,
            searchKey: "",
            pageNum: 1,
            pageSize: 10,
            total: 0,
            users: [],
        };
    },

    created() {
        this.fetchUsers();

    },
    methods: {
        search() {
            this.pageNum = 1;
            this.fetchUsers();
        },
        async fetchUsers() {
            const params = {
                pageNum: this.pageNum,
                pageSize: this.pageSize,
                account: this.searchKey,
                type: "admin"
            };
            try {
                const { data } = await this.$fetch.get("/api/user/list", params);
                this.users = data.sorts.map((item) => {
                    item.avatar = item.avatar || DEFAULTAVATAR;
                    return item
                });
                this.total = data.total;
            } catch (error) {
                console.log(error);
            }
        },

        handlePageChange(pageNum) {
            this.pageNum = pageNum;
            this.fetchUsers();
        },
        refresh() {
            this.handlePageChange(1);
        },
        opeUserModal(type, data) {
            this.visible = true;
            this.$nextTick(() => {
                this.$refs.edit.init(type, data)
            })
        },

        deleteUser(row) {
            const params = {
                id: row.id,
            };
            this.$confirm("确定要删除此用户嘛？", "", {
                distinguishCancelAndClose: true,
                confirmButtonText: "确认",
                cancelButtonText: "取消",
            }).then(async () => {
                const { code } = await this.$fetch.post("/api/user/delete", params);
                if (code === 0) {
                    this.$message.success("删除用户成功");
                }
                this.refresh();
            });
        },
    },
};
</script>

<style scoped lang="scss">
.avatar {
    width: 60px;
}

.user-management {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 50px);

    .user-management__header {
        display: flex;
        align-items: center;

        .user-management__header-main {
            height: 40px;
            display: flex;
            align-items: center;

            .management__headerInput {
                margin-right: 20px;
            }
        }
    }

    .user-management__content {
        display: flex;
        flex-direction: column;
        flex: 1;
        overflow: auto;
        padding-bottom: 0;

        .table-container {
            flex: 1;
            overflow: hidden;
            overflow-y: auto;
        }
    }

    .pagination-container {
        display: flex;
        justify-content: center;
        padding: 20px 0;

        .el-pagination {
            padding: 20px 0;
        }
    }
}

::v-deep .el-table {
    display: flex;
    height: 100%;
    /* overflow: auto; */
    flex-direction: column;

    .el-table__body-wrapper {
        flex: 1;
        overflow-y: auto;
    }

    .cell {
        width: 100%;
        height: 100%;
        padding: 0;
        padding-right: 10px;
    }
}
</style>
